﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

using SpectrEngine.Framework;

using CellWarzGame.Actors;
using CellWarzGame.Collections;
using SpectrEngine.Framework.Input;

namespace CellWarzGame.Screens
{
    public class GamePlayScreen
        : GameScreen, IPlayScreenService
    {
        const int SoundWaitTime = 6000;

        Texture2D backdrop;

        List<SoundEffect> sounds;

        int playNextSoundTime;

        Random rand = new Random();

        public GamePlayScreen(Game game)
            : base(game)
        {
            
            this.transitionOnTime = TimeSpan.FromSeconds(1.0);

            this.transitionMask = game.Content.Load<Texture2D>("Test\\organic_fade");

            game.Services.GetService<ScreenManager>().AddScreen(this, 0);

            // stupid hack:  if this service already exists, remove it, don't fully understand why this is so
            if (null != game.Services.GetService<IPlayScreenService>() )
            {
                game.Services.RemoveService<IPlayScreenService>();
                
            }
            game.Services.AddService(typeof(IPlayScreenService), this);

            Game.Services.GetService<ICellListService>().Clear();

            CellFactory.game = game;
            CellFactory.CreateCells(5, Cell.Side.blue, Cell.Controler.AI, new Rectangle(0, 0, 200, 200));
            CellFactory.CreateCells(5, Cell.Side.green, Cell.Controler.AI, new Rectangle(300, 0, 200, 200));
            CellFactory.CreateCells(5, Cell.Side.yellow, Cell.Controler.AI, new Rectangle(0, 300, 200, 200));
            CellFactory.CreateCells(5, Cell.Side.red, Cell.Controler.AI, new Rectangle(300, 300, 200, 200));

            Cell playa = new Virus(Game, Cell.Side.virus, Cell.Controler.player, new Vector2(640.0f, 385.0f), Cell.CellType.Virus);
            for (int i = 0; i < 5; i++)
            {
                Cell taila = new Cell(Game, Cell.Side.virus, Cell.Controler.Tail, new Vector2(460.0f, 300.0f + (50+(i*50))), Cell.CellType.Tail);
                taila.enemy = playa;
                playa = taila;
            }

            backdrop = game.Content.Load<Texture2D>(@"Background\CWZ_GamePlay_BG");
            LoadSounds();
        }

        private void LoadSounds()
        {
            sounds = new List<SoundEffect>();
            sounds.Add(Game.Content.Load<SoundEffect>("Sounds\\bubble1"));
            sounds.Add(Game.Content.Load<SoundEffect>("Sounds\\bubble2"));
            sounds.Add(Game.Content.Load<SoundEffect>("Sounds\\bubble3"));
            sounds.Add(Game.Content.Load<SoundEffect>("Sounds\\bubble4"));
            sounds.Add(Game.Content.Load<SoundEffect>("Sounds\\bubble5"));
            sounds.Add(Game.Content.Load<SoundEffect>("Sounds\\bubble6"));
            sounds.Add(Game.Content.Load<SoundEffect>("Sounds\\owl"));
            playNextSoundTime = rand.Next(SoundWaitTime / 2, SoundWaitTime);
        }

        public override void Update(GameTime gameTime)
        {

            int virusCount=0;
            int cellCount=0;

            if (GamePadManager.IsButtonReleased(PlayerIndex.One, GamePadButtons.RightShoulder))
            {
                // TODO:  this is weird.  you have to create, and remove in this order or it won't work
                new LevelCompleteScreen(Game);
                Game.Services.GetService<ScreenManager>().RemoveScreen(this);
            }


            if (GamePadManager.IsButtonReleased(PlayerIndex.One, GamePadButtons.LeftShoulder))
            {
                // TODO:  this is weird.  you have to create, and remove in this order or it won't work
                new GameOverScreen(Game);
                Game.Services.GetService<ScreenManager>().RemoveScreen(this);
            }


            //IList<DrawableGameNode> cellList
            IList<DrawableGameNode> cells = Game.Services.GetService<ICellListService>().GetNearest(new Vector2(0, 0));
            foreach (Cell c in cells)
            {
                if ((c is Virus) && (c.Action != Cell.ActionState.Death))
                {
                    virusCount++;
                }
                else if (c.cellType == Cell.CellType.Monk || c.cellType == Cell.CellType.Punk || c.cellType == Cell.CellType.Skunk) cellCount++;
            }

            if (virusCount == 0)
            {
                new GameOverScreen(Game);
                Game.Services.GetService<ScreenManager>().RemoveScreen(this);
            }
                
            if (cellCount == 0)
            {
                new LevelCompleteScreen(Game);
                Game.Services.GetService<ScreenManager>().RemoveScreen(this);               
            }

            PlayRandomSound(gameTime);
            base.Update(gameTime);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            Game.GraphicsDevice.Clear(Color.White);

            //spriteBatch.Begin();
            spriteBatch.Draw(backdrop, new Vector2(this.Game.GraphicsDevice.Viewport.Width / 2, this.Game.GraphicsDevice.Viewport.Height / 2), null, Color.White, 0.0f, new Vector2(backdrop.Width / 2, backdrop.Height / 2), 1.0f, SpriteEffects.None, 0.0f);
            //spriteBatch.End();


            base.Draw(gameTime);
        }

        private void PlayRandomSound(GameTime gameTime)
        {
            if (playNextSoundTime <= 0)
            {
                sounds[rand.Next(0, sounds.Count)].Play();
                playNextSoundTime = rand.Next(SoundWaitTime / 2, SoundWaitTime);
            }
            playNextSoundTime -= gameTime.ElapsedGameTime.Milliseconds;
        }
    }
}
